

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JPanel;

public class ImagePanel extends JPanel {

	  private Image img;
	  public boolean isPutImg = false;
	  private Graphics g=null;
	  private int x=-1;
	  private int y=-1;
	  private double currentAngle;
	  private Dimension theSize = new Dimension();
	  double w;
	  double h;
	  public ImagePanel(){
		  
	  }
	  
	  public boolean isPut(){
		  return isPutImg;
	  }
	  
	  public void setPutBoolean(boolean boo){
		  this.isPutImg = boo;
	  }

	  public void setImage(String img) {
		this.removeAll();
	    setImage(scale(img,400, 500));
	    isPutImg=true;
	  }

	  public void setImage(Image img) {
	    this.img = img;
	    theSize.setSize(img.getWidth(null), img.getHeight(null));
	    w = theSize.getWidth();
	    h = theSize.getHeight();

	    if(x==-1 || y==-1){
	    	this.setBounds(200 - (int)(this.img.getWidth(null)/2), 255 - (int)(this.img.getHeight(null)/2), (int)this.img.getWidth(null), (int)this.img.getHeight(null));
	    }else{
	    	this.setBounds(0,0, (int)this.img.getWidth(null), (int)this.img.getHeight(null));
	    }

	    isPutImg=true;
	  }
	  
	  public void setImage(BufferedImage img) {
	    this.img = scale(img,400,500);
	    
	    System.out.println(this.img.getWidth(null) + ":" + this.img.getHeight(null));
	    if(x==-1 || y==-1){
	    	this.setBounds(200 - (int)(this.img.getWidth(null)/2), 255 - (int)(this.img.getHeight(null)/2), (int)this.img.getWidth(null), (int)this.img.getHeight(null));
	    }else{
	    	this.setBounds(0,0, (int)this.img.getWidth(null), (int)this.img.getHeight(null));
	    }
	    isPutImg=true;
	  }


	  protected  void paintComponent(Graphics g) {
		super.paintComponent(g);
//		this.g = g;
//	    g.drawImage(img, 0, 0, null);
		if(img == null)return;
	    Graphics2D g2d = (Graphics2D) g;
		AffineTransform origXform = g2d.getTransform();
		AffineTransform newXform = (AffineTransform) (origXform.clone());
		int xRot = this.getWidth() / 2;
		int yRot = this.getHeight() / 2;
		newXform.rotate(Math.toRadians(currentAngle), xRot, yRot);
		g2d.setTransform(newXform);
		int x = (getWidth() - img.getWidth(this)) / 2;
		int y = (getHeight() - img.getHeight(this)) / 2;
		System.out.println("ImagePanel:"+getWidth()+":"+getHeight());
		System.out.println("ImagePanel2:"+img.getWidth(this)+":"+img.getHeight(this));
		g2d.drawImage(img, x, y, this);
		g2d.setTransform(origXform);
		System.out.println("ImagePanel:"+x+":"+y);
	  }
	  
	  public void RotateRight(double degre) {
			currentAngle = degre;
			if (currentAngle>= 360.0) {
				currentAngle = 0;
			}
	
			double cos = Math.abs(Math.cos(Math.toRadians(currentAngle)));
			double sin = Math.abs(Math.sin(Math.toRadians(currentAngle)));
	
			theSize.width = (int) (w * cos + h * sin);
			theSize.height = (int) (w * sin + h * cos);
	
			setSize(theSize);
	
			repaint();
		}

	  
	  /**
	   * 
	   * @param srcImageFile
	   * @param scale
	   * @param flag true 放大  false 縮小
	   */
	  private Image scale(String srcImageFile, double width, double height)  {  
		  double newwidth=0, newheight=0;
		  double divide;
	        try{  	        	
	            BufferedImage src = ImageIO.read(new File(srcImageFile));
	            if(src.getHeight() > src.getWidth()){
		            newheight = height;
		            divide = height / src.getHeight();
		            newwidth = src.getWidth() * divide;
	            }else{
	            	newwidth = width;
	            	divide = width / src.getWidth();
	            	newheight = src.getHeight() * divide;
	            }
	            Image image = src.getScaledInstance((int)newwidth, (int)newheight, Image.SCALE_DEFAULT); 
	            return image;	       
	        }catch (IOException e){  
	            e.printStackTrace();  
	            return new ImageIcon(srcImageFile).getImage();
	        }  
	  }  
	  
	  private Image scale(BufferedImage src, double width, double height)  {  
		  double newwidth=0, newheight=0;
		  double divide;
            if(src.getHeight() > src.getWidth()){
	            newheight = height;
	            divide = height / src.getHeight();
	            newwidth = src.getWidth() * divide;
            }else{
            	newwidth = width;
            	divide = width / src.getWidth();
            	newheight = src.getHeight() * divide;
            }
            return src.getScaledInstance((int)newwidth, (int)newheight, Image.SCALE_DEFAULT);        
	        
	  } 
	  
	  
	  public void setXY(int x,int y){
		  this.x = x;
		  this.y = y;
	  }
	  
	  public Dimension getPreferredSize() {
			return this.theSize;
		}
 
	  public void clearImg(){
		  if(img != null)
			  img.flush();
		  img = null;
		  isPutImg = false;
		  if(g != null)
			  g.dispose();
		  g=null;
		  
	  }

}
